<?php

/**
 * 小程序服务商管理系统 v1.0.1
 *
 * 商业运营到axzz.cn官网进行授权使用。
 *
 * (c) cighsen02 <xiayu@959602.com>
 *
 * Create at: 2025.05.03
 * Update at: 2025.11.23
 */

namespace App\Services\Auth;

use App\Models\User;
use Laravel\Passport\RefreshTokenRepository;
use Illuminate\Support\Facades\Auth;
use InvalidArgumentException;

/**
 * 会员注册、登陆、退出服务层
 */
class Guest extends UserService
{
    public function __construct(
        protected RefreshTokenRepository $refreshTokenRepository
    )
    {}

    /**
     * 注册用户
     *
     * @param array $data
     * @param string $role
     * @return User
     */
    public function register(array $data, string $role = 'member'): User
    {
        return $this->create($data, $role);
    }

    /**
     * 登出
     *
     * @return void
     *
     * @throws InvalidArgumentException
     */
    public function destroy(): void
    {
        if (null === $token = Auth::user()?->token()) {

            throw new InvalidArgumentException('Invalid credentials');
        }

        $token->revoke();

        $this->refreshTokenRepository->revokeRefreshTokensByAccessTokenId($token->id);
    }
}
